<?php
class LZ_Photo
{
	var $table = 'photo';
	var $group_table = 'photo_group';
	var $id = 'photo_id';
	var $category_id = 'category_id';
	var $title = 'title';
	var $shotdate = 'shotdate';
	var $shot_location = 'shot_location';
	var $update_time = 'update_time';
	var $tag = 'tag';
	var $description = 'description';
	var $file_path = 'file_path';
	var $file_type = 'file_type';
	var $options = 'options';
	var $more_options = 'more_options';
	var $user_id = 'user_id';
	var $orig_size = 'orig_size';
	var $status = 'status';
	var $author = 'author';
	var $small = 'small';
	var $big = 'big';

	function LZ_Photo()
	{
		$this->table = LZ_MYSQL_PREFIX.$this->table;
	}

	function add( $data )
	{
		global $db;
		//$data['status'] = 'unpublished';
		$data['update_time'] = time();
		$sql = make_insert_sql($this->table, $data);
		return $db->query($sql);
	}

	function exists($md5)
	{
		global $db;
		$sql = "SELECT * FROM `$this->table` WHERE `md5` = '".$md5."';";
		$r = $db->query($sql);

		return ($db->num_rows($r) > 0);
	}

	function get_list($data=array())
	{
		global $db;
		//select
		$_s = $data['select']?$data['select']:'*';
		$sql = "SELECT $_s FROM `{$this->table}` WHERE 1=1 ";
		
		//where
		$data['title'] && $sql.=" AND `title` LIKE '%".$data['title']."%' ";
		$data['tag'] && $sql.=" AND `tag` LIKE '%".$data['tag']."%' ";
		$data['description'] && $sql.=" AND `description` LIKE '%".$data['description']."%' ";
		$data['category_id'] && $sql.=" AND `category_id`='".$data['category_id']."' ";
		
		if (isset($data['status']))
			$sql.=" AND `status`='".$data['status']."' ";

		if (isset($data['photo_id']))
			$sql.=" AND `$this->id`='".$data['photo_id']."' ";
		if (isset($data['from_time']))
			$sql.=" AND `update_time`>'".$data['from_time']."' ";
		if (isset($data['to_time']))
			$sql.=" AND `update_time`<'".$data['to_time']."'";
			
		//order by
		$sql.= " ORDER BY ";
		if($data['order'])
		{
			$sql.= $data['order'];
			$sql.= ($data['asc'])?' '.$data['asc'].' ':' DESC ';
			if($data['order'] != 'update_time') $sql.=",update_time DESC ";
		}
		else $sql.= "update_time DESC ";
		
		if ($data['from'] && $data['total'])
			$sql.=" LIMIT ".$data['from'].",".$data['total'];
		else if ($data['total'])
			$sql.=" LIMIT ".$data['total'];
		$sql.= ';';
		return $db->get_all($sql);
	}

	function get_one($id)
	{
		global $db;
		$sql = "SELECT * FROM $this->table WHERE $this->id = '$id' LIMIT 1";
		return $db->get_one($sql);
	}

	function delete($data = array())
	{
		global $db;
		if(!is_array($data))
			$data = array($data);
		$ids = '';
		foreach($data as $id)
			$ids .= $id.',';
		$ids = substr($ids, 0, -1);
		$sql = "DELETE FROM $this->table WHERE $this->id IN ($ids)";
		if ( $db->query($sql))
		{
			include_once(LZ_BASEPATH.'model/search.php');
			$search = new LZ_Search;
			foreach($data as $id)
			{
				$search->delete('photo',$id);
			}
		}
	}
	
	function add_download($id)
	{
		global $db;
		if ($id > 0)
		{
			$sql = "UPDATE $this->table SET downloaded = downloaded+1 WHERE $this->id='$id'";
			return $db->query($sql);
		}
		else return false;
	}
	
	function update($id, $data = array())
	{
		global $db;
		if ($id <= 0) return;
		$data['update_time'] = time();
		$sql = make_update_sql($this->table, $data, array($this->id => $id));
		if ($db->query($sql))
		{
			include_once(LZ_BASEPATH.'model/search.php');
			$search = new LZ_Search;
			$s = $data['tag'].' '.$data['title'];
			if ($s) $search->add('photo',$id,$s);
		}
	}
}
?>